package xin.jishu.ai.providers;

import org.openstack4j.api.Builders;
import org.openstack4j.model.compute.QuotaSet;
import org.openstack4j.model.compute.QuotaSetUpdate;
import org.openstack4j.model.identity.v3.Project;
import org.openstack4j.model.storage.block.BlockQuotaSet;
import xin.jishu.ai.entities.Student;

/**
 * @author sxsx欧克 <wo@jishu.xin>
 */
public class Quota extends BaseProvider {

    public static final int LIMIT_MEMORY = 16 * 1024;

    @Override
    public void initialize() {

    }

    @Override
    public boolean exists(Student student) {
        Project project = KeyStone.getOrCreateProject(student);

        QuotaSet quota = KeyStone.client.compute()
                .quotaSets()
                .get(project.getId());

        return quota.getCores() == 10
                && quota.getRam() == Quota.LIMIT_MEMORY
                && quota.getFloatingIps() == 2
                && quota.getInstances() == 3
                && quota.getVolumes() == 3
                && quota.getGigabytes() == 120;
    }

    @Override
    public void create(Student student) {
        Project project = KeyStone.getOrCreateProject(student);
        // Nova
        QuotaSetUpdate quotaSetUpdate = Builders.quotaSet()
                .ram(Quota.LIMIT_MEMORY)
                .cores(10)
                .instances(3)
                .floatingIps(2)
                .build();

        KeyStone.client.compute()
                .quotaSets()
                .updateForTenant(project.getId(), quotaSetUpdate);
        // Cinder
        BlockQuotaSet blockQuotaSet = Builders.blockQuotaSet()
                .volumes(3)
                .gigabytes(120)
                .build();

        KeyStone.client.blockStorage()
                .quotaSets()
                .updateForTenant(project.getId(), blockQuotaSet);
    }

}
